// 直播服务实现类
package com.neuedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neuedu.dao.*;
import com.neuedu.model.*;
import com.neuedu.service.LiveStreamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Service
public class LiveStreamServiceImpl implements LiveStreamService {
    @Autowired
    private LiveStreamDao liveStreamDao;
    @Autowired
    private LiveProductDao liveProductDao;
    @Autowired
    private LiveInteractionDao liveInteractionDao;
    @Autowired
    private LiveLotteryDao liveLotteryDao;
    @Autowired
    private LiveLotteryRecordDao liveLotteryRecordDao;
    @Autowired
    private ProductDao productDao;

    @Override
    public List<LiveStream> getAllLiveStreams() {
        return liveStreamDao.selectList(null);
    }

    @Override
    public LiveStream getLiveStreamById(Integer lsid) {
        return liveStreamDao.selectById(lsid);
    }

    @Override
    public void addLiveStream(LiveStream liveStream) {
        liveStreamDao.insert(liveStream);
    }

    @Override
    public void updateLiveStream(LiveStream liveStream) {
        liveStreamDao.updateById(liveStream);
    }

    @Override
    public void deleteLiveStream(Integer lsid) {
        liveStreamDao.deleteById(lsid);
    }

    @Override
    public List<Product> getLiveProducts(Integer lsid) {
        List<LiveProduct> liveProducts = liveProductDao.selectList(new QueryWrapper<LiveProduct>().eq("lsid", lsid));
        List<Product> products = new ArrayList<>();
        for (LiveProduct liveProduct : liveProducts) {
            products.add(productDao.selectById(liveProduct.getPid()));
        }
        return products;
    }

    @Override
    public void addLiveProduct(LiveProduct liveProduct) {
        liveProductDao.insert(liveProduct);
    }

    @Override
    public List<LiveInteraction> getLiveInteractions(Integer lsid) {
        return liveInteractionDao.selectList(new QueryWrapper<LiveInteraction>().eq("lsid", lsid));
    }

    @Override
    public void addLiveInteraction(LiveInteraction liveInteraction) {
        liveInteractionDao.insert(liveInteraction);
    }

    @Override
    public List<LiveLottery> getLiveLotteries(Integer lsid) {
        return liveLotteryDao.selectList(new QueryWrapper<LiveLottery>().eq("lsid", lsid));
    }

    @Override
    public void addLiveLottery(LiveLottery liveLottery) {
        liveLotteryDao.insert(liveLottery);
    }

    @Override
    public boolean participateLottery(Integer llid, Integer uid) {
        LiveLotteryRecord record = new LiveLotteryRecord();
        record.setLlid(llid);
        record.setUid(uid);
        Random random = new Random();
        boolean win = random.nextBoolean();
        record.setWin(win ? 1 : 0);
        liveLotteryRecordDao.insert(record);
        return win;
    }
}













